<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace core\utils;

class RandomUtil
{
    private const CHARACTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

    private const CHAR_LETTER = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    private const CHAR_INT = '0123456789';

    /**
     * @param int $length
     * @return string
     */
    public static function randomStr($length = 6): string
    {
        $s = '';
        for ($i = 0; $i < $length; ++$i) {
            $s .= self::CHARACTERS[rand(0, strlen(self::CHARACTERS) - 1)];
        }
        return $s;
    }

    /**
     * @param int $length
     * @return string
     */
    public static function randomLetter($length = 6): string
    {
        $s = '';
        for ($i = 0; $i < $length; ++$i) {
            $s .= self::CHAR_LETTER[rand(0, strlen(self::CHAR_LETTER) - 1)];
        }
        return $s;
    }

    /**
     * @param int $length
     * @return string
     */
    public static function randomInt($length = 4): string
    {
        $s = '';
        for ($i = 0; $i < $length; ++$i) {
            $s .= self::CHAR_INT[rand(0, strlen(self::CHAR_INT) - 1)];
        }
        return $s;
    }

    static function UUID($prefix = "")
    {
        $chars = md5(uniqid(strval(mt_rand()), true));
        $uuid = substr($chars, 0, 8) . '-'
            . substr($chars, 8, 4) . '-'
            . substr($chars, 12, 4) . '-'
            . substr($chars, 16, 4) . '-'
            . substr($chars, 20, 12);
        return $prefix . strtolower($uuid);
    }
}
